package com.kuaiditu.assistant.api.assistant_api.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.kuaiditu.assistant.common.Constants;
import com.kuaiditu.assistant.util.MD5;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Eenie on 2017/7/14.
 * Email: eeniework@gmail.com
 * Des:
 */

public class AssistantRequest {


    private AssistantRequest(String method, Object data) {
        params = new HashMap<>();
        this.data = data;
        this.method = method;
    }

    public static AssistantRequest DEFAULT(String method, Object data) {
        AssistantRequest request = new AssistantRequest(method, data);
        request.setApiVersion("1");
        request.setAppId(Constants.PRIVATEKEY);
        request.setMethod(method);
        request.setData(data);
        request.setTimestamp(String.valueOf(System.currentTimeMillis()));
        request.sign(method, data);
        return request;
    }


    public static final String PARAM_FIELD_APPID = "appId";
    public static final String PARAM_FIELD_SIGN = "sign";
    public static final String PARAM_FIELD_TIMESTAMP = "timestamp";
    public static final String PARAM_FIELD_DATA = "data";
    public static final String PARAM_FIELD_METHOD = "method";
    public static final String PARAM_FIELD_APIVERSION = "apiVersion";


    private Map<String, Object> params;
    private Object data;
    private String method;


    //强制登录
    @JSONField(serialize = false)
    private boolean forceLogin;
    //需要登录
    @JSONField(serialize = false)
    private boolean needToken;
    @JSONField(serialize = false)
    private String tokenKeyName = "user_id";


    private void setAppId(String appId) {
        addExtraField(PARAM_FIELD_APPID, appId);
    }


    private void setSign(String sign) {
        addExtraField(PARAM_FIELD_SIGN, sign);
    }


    private void setTimestamp(String timestamp) {
        addExtraField(PARAM_FIELD_TIMESTAMP, timestamp);
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
        addExtraField(PARAM_FIELD_DATA, data);
    }

    public void setMethod(String method) {
        addExtraField(PARAM_FIELD_METHOD, method);
    }


    private void setApiVersion(String apiVersion) {
        addExtraField(PARAM_FIELD_APIVERSION, apiVersion);
    }


    private void sign(String method, Object data) {
        String src = method + Constants.PRIVATEKEY + JSON.toJSONString(data);
        String sign = MD5.getMD5Str(src);
        setSign(sign);
    }


    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }


    public boolean isForceLogin() {
        return forceLogin;
    }


    public boolean isNeedToken() {
        return needToken;
    }

    public AssistantRequest UserFlag(boolean needToken, boolean forceLogin) {
        this.needToken = needToken;
        this.forceLogin = forceLogin;
        return this;
    }

    public AssistantRequest addExtraField(String key, Object value) {
        params.put(key, value);
        return this;
    }


    public JSONObject getDataJson() {
        return JSON.parseObject(JSON.toJSONString(data));
    }


    public AssistantRequest addDataExtraField(String key, Object value) {
        setData(getDataJson().fluentPut(key, value));
        sign(method, data);
        return this;
    }

    public String build() {
        return JSON.toJSONString(params);
    }


    public String getTokenKeyName() {
        return tokenKeyName;
    }

    public AssistantRequest setTokenKeyName(String tokenKeyName) {
        this.tokenKeyName = tokenKeyName;
        return this;
    }

    public Map<String, Object> getParams() {
        return params;
    }
}
